import { $cancelAnimationFrame, $requestAnimationFrame, $window, AmbientLight, PointLight, DirectionalLight, PerspectiveCamera, PLATFORM, Scene, sRGBEncoding, WebGL1Renderer, TextureLoader, LinearEncoding, Color } from 'three-platformize'
import { WechatPlatform } from 'three-platformize/src/WechatPlatform'
// import { GLTF, GLTFLoader } from 'three-platformize/examples/jsm/loaders/GLTFLoader'
import { OBJLoader } from 'three-platformize/examples/jsm/loaders/OBJLoader';
import { MTLLoader } from 'three-platformize/examples/jsm/loaders/MTLLoader';
import { OrbitControls } from 'three-platformize/examples/jsm/controls/OrbitControls'

Page({
  modelId: '',
  modelSrc: '',
  mtlSrc: '',
  disposing: false,
  platform: null as unknown as WechatPlatform,
  frameId: -1,
  videoObj:{
    src:''
  },
  // onToMoney() {
  //   wx.navigateTo({
  //     url: '/pages/order/placeOrder?pageType=matrix&id=' + this.modelId,
  //   })
  // },
  onLoad(options) {
    console.log('模型参数：', options)
    this.modelId = options.modelId;
    this.modelSrc = decodeURIComponent(options.modelSrc);
    this.mtlSrc=this.modelSrc.replace('.obj','.mtl');
    this.videoObj.src=decodeURIComponent(options.videoSrc);

    // this.modelSrc = 'https://ders-1312669922.cos.ap-guangzhou.myqcloud.com/10000005419162/%E4%BA%9A%E8%BF%90%E6%9D%91227S-3-1-18cm-%E4%BF%9E%E9%83%A8%E9%95%BF.obj';
    // this.mtlSrc=this.modelSrc.replace('.obj','.mtl');
    // this.videoObj.src='https://mpvideo.qpic.cn/0bc32uaaaaaaruadrfdiffsvbvodadkqaaaa.f10002.mp4?dis_k=f994716eea3b07d38149b9016377751e&dis_t=1703225629&play_scene=10600&auth_info=CYyGppJPHHB5r6zm3mV0b04yDSZvOUwaOzdVZ2UvQ2wvTzEe&auth_key=b073435a0df8e47d402f46dd72f8d6e9&spec_id=undefined1703225629&vid=wxv_3216808252840361988&format_id=10002'
  },
  async onReady() {

    wx.showLoading({ mask: false, title: '加载中' })

    wx.createSelectorQuery().select('#gl').node().exec(async (res) => {
      const canvas = res[0].node

      this.platform = new WechatPlatform(canvas)
      PLATFORM.set(this.platform);

      const renderer = new WebGL1Renderer({ canvas, antialias: true, alpha: true });
      const camera = new PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1000);
      const scene = new Scene();
      const textureLoader = new TextureLoader();
      const uvMap = await textureLoader.loadAsync(
        'https://techbrood.com/threejs/examples/textures/UV_Grid_Sm.jpg',
      );

      let mtlLoader = new MTLLoader();

      const materials = await mtlLoader.loadAsync(
        this.mtlSrc,
      );
      materials.preload();

      const objLoader = new OBJLoader();
      objLoader.setMaterials(materials);

      const object = await objLoader.loadAsync(
        this.modelSrc,
      );

      scene.add(object);


      const controls = new OrbitControls(camera, canvas);
      controls.enableDamping = true;


      // camera.position.z = 10;
      // scene.position.z = -3;
      // scene.background = new Color(0xffffff);
      renderer.outputEncoding = LinearEncoding;
      scene.add(new AmbientLight(0xffffff, .8));
      camera.add(new PointLight(0xffffff, .8));
      // scene.add(new DirectionalLight(0xffffff, 1.0));
      renderer.setSize(canvas.width, canvas.height);
      renderer.setPixelRatio(2);
      renderer.setClearColor(0xffffff, .6);

      // renderer.setPixelRatio($window.devicePixelRatio);
      object.traverse(function (child) {

        // @ts-ignore
        // if (child.isMesh) child.material.map = uvMap;
      });
      // object.position.y = -95;
      // camera.position.z = 200;
      object.position.y = 0;
      camera.position.z = 30;

      const render = () => {
        if (!this.disposing) this.frameId = $requestAnimationFrame(render)
        controls.update()
        renderer.render(scene, camera);
      };
      render();
      wx.hideLoading();
    })
  },

  onUnload() {
    this.disposing = true
    $cancelAnimationFrame(this.frameId)
    PLATFORM.dispose()
  },

  onTX(e: any) {
    this.platform.dispatchTouchEvent(e)
  },
})
